package cn.shyouchi.wuguanjia.android.ui.event.proprietor;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.OwnerEntity;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class EventProprietorActivity extends BaseActivity implements ProprietorInfoContract.View {


    public final static int REQUEST_TYPE_ADD_PROPRIETOR = 10001;
    public final static int REQUEST_TYPE_ADD_PROPRIETOR_SUCCESS = 10002;


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_proprietor)
    RecyclerView mRvProprietor;
    @BindView(R.id.btn_add_proprietor)
    Button mBtnAddProprietor;
    ProprietorAdapter mAdapter;
    List<OwnerEntity.ProprietorBean> proprietors = new ArrayList<>();
    EventProprietorPresenter mProprietorPresenter;
    boolean needRefresh = true;
    String projectId;

    @Override
    public int initContentView() {
        return R.layout.activity_event_proprietor;
    }

    @Override
    public void initInjector() {
        mProprietorPresenter = new EventProprietorPresenter(mContext, this);
        projectId = getIntent().getStringExtra("projectId");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        mRvProprietor.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new ProprietorAdapter(proprietors, this);
        mRvProprietor.setAdapter(mAdapter);
        EventBus.getDefault().register(this);
    }


    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @OnClick(R.id.btn_add_proprietor)
    public void addProprietor() {
        Intent intent = new Intent(mContext, AddProprietorActivity.class);
        intent.putExtra("projectId", projectId);
        startActivityForResult(intent, REQUEST_TYPE_ADD_PROPRIETOR);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_TYPE_ADD_PROPRIETOR && resultCode == REQUEST_TYPE_ADD_PROPRIETOR_SUCCESS) {
            mProprietorPresenter.getOwners(projectId);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (needRefresh) {
            mProprietorPresenter.getOwners(projectId);
            needRefresh = false;
        }
    }


    @Override
    public void onOwnersRefresh(List<OwnerEntity.ProprietorBean> proprietors) {
        this.proprietors.clear();
        this.proprietors.addAll(proprietors);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(OwnerEntity.ProprietorBean proprietor) {
        LogUtil.json(proprietor);
        Intent intent = new Intent(mContext, PreviewProprietorActivity.class);
        intent.putExtra("projectId", projectId);
        intent.putExtra("proprietor", proprietor);
        startActivityForResult(intent, REQUEST_TYPE_ADD_PROPRIETOR);
    }

    @Override
    public void onDelClick(OwnerEntity.ProprietorBean proprietor) {
        mProprietorPresenter.delOwner(projectId, String.valueOf(proprietor.getId()));
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DataChangeEvent(DataChangeEvent event) {
        needRefresh = true;
    }


}
